import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)
import Login from '../views/Login.vue';
import {verifyToken} from '@/apis/user'

const routes = [
  {
    path: '/',
    name: 'login',
    component: Login
  },
  {
    path:'/home',
    name:'home',
    component:()=> import('../views/Home.vue'),
    children:[
      {
        path:'/home/main',
        name:'home/main',
        component:()=>import('../views/homemodules/HomeMain.vue')
      },
      {
        path:'/home/ordermanagement',
        name:'home/ordermanagement',
        component:()=>import('../views/homemodules/OrderManagement.vue')
      },
      {
        path:'/home/commmanagement',
        name:'home/commmanagement',
        component:()=>import('../views/homemodules/CommManagement.vue'),
        children:[{
          path:'/home/commmanagement/commlist',
          name:'home/commmanagement/commlist',
          component:()=>import('../views/homemodules/commmoudles/CommList.vue')
      },{
        path:'/home/commmanagement/commadd',
        name:'home/commmanagement/commadd',
        component:()=>import('../views/homemodules/commmoudles/CommAdd.vue')
    },{
      path:'/home/commmanagement/commclass',
      name:'home/commmanagement/commclass',
      component:()=>import('../views/homemodules/commmoudles/CommClass.vue')
  }]
      },
      {
        path:'/home/storemanagement',
        name:'home/storemanagement',
        component:()=>import('../views/homemodules/StoreManagement.vue')
      },
      {
        path:'/home/idmanagement',
        name:'home/idmanagement',
        component:()=>import('../views/homemodules/IdManagement.vue'),
        children:[{
          path:'/home/idmanagement/idlist',
          name:'home/idmanagement/idlist',
          component:()=>import('../views/homemodules/idmodules/IDList.vue')
        },{
          path:'/home/idmanagement/addid',
          name:'home/idmanagement/addid',
          component:()=>import('../views/homemodules/idmodules/AddID.vue')
        },{
          path:'/home/idmanagement/amendid',
          name:'home/idmanagement/amendid',
          component:()=>import('../views/homemodules/idmodules/AmendID.vue')
        }]
      },
      {
        path:'/home/salesstatics',
        name:'home/salesstatics',
        component:()=>import('../views/homemodules/SalesStatistics.vue'),
        children:[{
          path:'/home/salesstatics/orderstatistics',
          name:'home/salesstatics/orderstatistics',
          component:()=>import('../views/homemodules/salesmoudles/OrderStatistics.vue')
        },{
          path:'/home/salesstatics/commstatistics',
          name:'home/salesstatics/commstatistics',
          component:()=>import('../views/homemodules/salesmoudles/CommStatistics.vue')
        }]
        
      },{
        path:'/home/userinfo',
        name:'home/userinfo',
        component:()=>import('../views/homemodules/UserInfo.vue')
      }

    ]
    
  },
  {
    path:'*',
    component:()=>import('../views/Error.vue')
  }
  // {
  //   path: '/about',
  //   name: 'About',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  // }
]

const router = new VueRouter({
  routes
})

//路由发生变化前进行拦截
router.beforeEach((to,from,next)=>{
      if(to.path!='/'){
        verifyToken({token:localStorage.token}).then(res=>{
          if(res.data.code!=0){
              next('/')
          }else{
            next()
          }
        })
      }else{
        next()  //放行
      }
   
})

export default router
